package com.pattern.proxy;

/**
 * @author Mr.yuan
 * @version v1.0
 * @date 2019/8/2 10:21
 **/
public class ProxyPattern {
    public static void main(String[] args) {
        Image image = new ProxyImage("hello");
        image.display();
        System.out.println("===");
        image.display();
    }

    private interface Image{
        void display();
    }
    private static class RealImage implements Image{
        private String fileName;

        RealImage(String fileName) {
            this.fileName = fileName;
            loadFromDisk(fileName);
        }

        private void loadFromDisk(String fileName) {
            System.out.println("from disk load "+fileName);
        }

        @Override
        public void display() {
            System.out.println("display "+fileName);
        }
    }
    private static class ProxyImage implements Image{
        private RealImage realImage;
        private String fileName;

        ProxyImage(String fileName) {
            this.fileName = fileName;
        }

        @Override
        public void display() {
            if (null == realImage){
                realImage = new RealImage(fileName);
            }
            realImage.display();
        }
    }
}
